package com.paypalpay.turntointerfaceservice;

import com.paypalpay.entity.common.UnifiedOrderPackages;
import com.paypalpay.pojo.Constant;
import com.paypalpay.pojo.request.RequestBody.OrderMonthlyPackageRequestBody;
import com.paypalpay.pojo.request.RequestHeader;
import com.paypalpay.pojo.request.subRequest.OrderMonthlyPackageRequest;
import com.paypalpay.pojo.response.subResponse.OrderMonthlyPackageResponse;
import com.paypalpay.service.PartnerService;
import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.wx.HttpClientTool;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Demo 去北京下实际的单时的实现类
 *
 * @author lm
 * @date 2018/08/31
 */
public class TurnToOrderService {
    @Autowired
    PartnerService partnerService;

    public UnifiedOrderPackages orderMonthlyPackage(String partnerCode, String deviceId, String deviceType, String packageName, String packageCode, String orderPeriod, String payAmount, String isOpen, String secret) {
        try {
            // 请求参数的处理
            OrderMonthlyPackageRequestBody body = new OrderMonthlyPackageRequestBody();
            body.setDeviceId(deviceId);
            body.setDeviceType(deviceType);
            body.setGlobalOrder("0");
            body.setPackageName(packageName);
            body.setPackageCode(packageCode);
            body.setPackageType("1");
            body.setOrderPeriod(orderPeriod);
            body.setPayRst("1");
            body.setPayType("1");
            body.setPayAmount(payAmount);
            body.setIsOpen("0");

            // 请求参数拼接
            OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_orderMonthlyPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            System.out.println("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(request, "utf-8");
            System.out.println("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("返回结果：" + result);

            OrderMonthlyPackageResponse responseObj = new JaxbUtil(OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                System.out.println("信息是什么？" + retCode);
                // 成功:北京回传的orderId
                String orderId = responseObj.getBody().getOrderId();
                System.out.println("回传的orderId" + orderId);
                String startData = responseObj.getBody().getOrderStartDate();
                String expireDate = responseObj.getBody().getOrderExpireDate();

                UnifiedOrderPackages user = new UnifiedOrderPackages();
                user.setOrderId(orderId);
                user.setOrderStartDate(startData);
                user.setOrderExpireDate(expireDate);
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                UnifiedOrderPackages user = new UnifiedOrderPackages();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
